/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.templet.web;

import com.alibaba.fastjson.JSONObject;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.templet.entity.EleTemplet;
import com.thinkgem.jeesite.modules.templet.entity.EleTempletSource;
import com.thinkgem.jeesite.modules.templet.service.EleTempletService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模板管理Controller
 * @author ZC
 * @version 2017-04-27
 */
@Controller
@RequestMapping(value = "${adminPath}/templet/eleTemplet")
public class EleTempletController extends BaseController {

	@Autowired
	private EleTempletService eleTempletService;

	@ModelAttribute
	public EleTemplet get(@RequestParam(required=false) String id) {
		EleTemplet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleTempletService.get(id);
		}
		if (entity == null){
			entity = new EleTemplet();
		}
		return entity;
	}

	@RequiresPermissions("templet:eleTemplet:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleTemplet eleTemplet, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleTemplet> page = eleTempletService.findPage(new Page<EleTemplet>(request, response), eleTemplet); 
		model.addAttribute("page", page);
		return "modules/templet/eleTempletList";
	}

	@RequiresPermissions("templet:eleTemplet:view")
	@RequestMapping(value = "form")
	public String form(EleTemplet eleTemplet, Model model) {
		model.addAttribute("eleTemplet", eleTemplet);
        model.addAttribute("eleTempletSource", new EleTempletSource());
		return "modules/templet/eleTempletForm";
	}

    @RequiresPermissions("templet:eleTemplet:edit")
    @RequestMapping(value = "save")
    public String save(EleTemplet eleTemplet, Model model) {
        if (!beanValidator(model, eleTemplet)){
            return form(eleTemplet, model);
        }
        eleTempletService.save(eleTemplet,null);
        // 生成代码
        if("0".equals(eleTemplet.getFlag())){
            return "redirect:" + Global.getAdminPath() + "/templet/eleTemplet/selectSourceForm?templetId="+eleTemplet.getId();
        }
        else{
            return "redirect:" + Global.getAdminPath() + "/templet/eleTemplet/?repage";
        }
    }

	@RequiresPermissions("templet:eleTemplet:edit")
	@RequestMapping("selectSource")
	public String selectSource(EleTemplet eleTemplet, Model model) {
        eleTempletService.save(eleTemplet,null);

        // 生成代码
        if("0".equals(eleTemplet.getFlag())){
            return "redirect:" + Global.getAdminPath() + "/templet/eleTemplet/designTemplet?id="+eleTemplet.getId();
        }
        else{
            return "redirect:" + Global.getAdminPath() + "/templet/eleTemplet/?repage";
        }
	}

    @RequiresPermissions("templet:eleTemplet:edit")
    @RequestMapping("designTemplet")
    public String designTemplet(EleTemplet eleTemplet, Model model) {
        model.addAttribute("eleTemplet", eleTemplet);
        return "modules/templet/designTempletForm";
    }

    @RequiresPermissions("templet:eleTemplet:edit")
    @RequestMapping("saveTempeteSource")
    @ResponseBody
    public String saveTempeteSource(String eleTempletId,
                                    String name,
                                    int width,
                                    int height,
                                    String remarks,
                                    String eleTempletSourceList){
        eleTempletSourceList = eleTempletSourceList.replace("&quot;","\"");

        EleTemplet entity = new EleTemplet(eleTempletId);
        entity.setName(name);
        entity.setWidth(width);
        entity.setHeight(height);
        entity.setRemarks(remarks);

        List<EleTempletSource> templetSourceList = new ArrayList<>();
        Map map = JsonMapper.getInstance().fromJson(eleTempletSourceList, Map.class);
        for(Object item : map.keySet()){
            Map mapItem= (Map)map.get(item);

            EleTempletSource templetSource = JSONObject.parseObject(JSONObject.toJSONString(mapItem), EleTempletSource.class);
            templetSourceList.add(templetSource);
        }
        entity.setEleTempletSourceList(templetSourceList);

        eleTempletService.save(entity, "position");

        return "success";
    }

    @RequiresPermissions("templet:eleTemplet:edit")
	@RequestMapping(value = "delete")
	public String delete(EleTemplet eleTemplet, RedirectAttributes redirectAttributes) {
		eleTempletService.delete(eleTemplet);
		addMessage(redirectAttributes, "删除模板成功");
		return "redirect:"+Global.getAdminPath()+"/templet/eleTemplet/?repage";
	}

}